package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.log4j.Logger.getLogger;

/**
 * 用户模块
 */
@Controller
@RequestMapping(value = "/user")
@Scope("prototype")
public class SysUserController {
    private static final Logger logger = getLogger(SysUserController.class);

    @Resource
    private SysUserService sysUserService;

    @ExceptionHandler(value = {RuntimeException.class})
    public String handleException(RuntimeException e, HttpServletRequest req) {
        req.setAttribute("error", e);
        return "error";
    }
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }

    @RequestMapping("/exLogin")
    public String exLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        SysUser user = sysUserService.login(username, password);
        if (null == user) {
            throw new RuntimeException("用户名或密码错误");
        }
        return "redirect:/user/toMain";
    }

    @RequestMapping("/toLogin")
    public String login() {
        return "login";
    }

//    @RequestMapping("/login")
//    public String doLogin(
//            @RequestParam("username") String username,
//            @RequestParam("password") String password,
//            HttpSession session,
//            HttpServletRequest request) {
//        logger.debug("进入登录方法");
//        SysUser sysUser = sysUserService.login(username, password);
//        if (sysUser != null) {
//            session.setAttribute(Constants.USER_SESSION, sysUser);
//            return "redirect:/user/toMain";
//        } else {
//            request.setAttribute("error", "用户名或密码错误");
//            return "login";
//        }
//    }

//    @RequestMapping("/toMain")
//    public String main(HttpSession session) {
//        if (session.getAttribute(Constants.USER_SESSION) == null) {
//            return "redirect:/user/toLogin";
//        }
//        return "frame";
//    }
//    @GetMapping("/list")
//    public String getUserList(Model model,String queryRealName,Integer queryRoleId,@RequestParam(defaultValue = "1")Integer pageIndex) {
//        List<SysUser> userList=null;
//        try {
//            int pageSize= Constants.pageSize;
//            int totalCount=sysUserService.getUserCount(queryRealName,queryRoleId);
//            PageSupport pages=new PageSupport();
//            pages.setPageSize(pageSize);
//            pages.setTotalCount=pages.getTotalCount();
//            if(pageIndex>totalCount) {
//                pageIndex = totalCount;
//                throw new RuntimeException("页码不正确");
//            }
//            userList
//= sysUserService.getUserList(queryRealName, queryRoleId, pageIndex, pageSize);
//            model.addAttribute("userList", userList)
//              List<SysRole> roleList=null;
//              roleList=sysRoleService.getRoleList();
//            model.addAttribute("roleList", roleList);
//            model.addAttribute("queryRealName", queryRealName);
//            model.addAttribute("queryRoleId", queryRoleId);
//            model.addAttribute("pages", pages);
//
//            } catch (Exception e) {
//                  e.printStackTrace();
//                logger.error("获取用户列表失败", e);
//            return "redirect:/user/toSysError";";
//        }
//      return "sysUser/list";
//    }
    private List<SysUser> userList = new ArrayList<>();
    private List<SysUser> queryUserList = new ArrayList<>();

    public SysUserController() {
        try {
            userList.add(new SysUser(
                    1,
                    "wanglulu",
                    "王璐璐刘",
                    "0000000",
                    2,
                    new Date(),
                    "18567542321",
                    "北京市朝阳区北苑家园茉莉园20号楼",
                    1,
                    1,
                    new Date(),
                    2,
                    new Date(),
                    40,
                    "黄卫华刘"
            ));
            userList.add(new SysUser(
                    2,
                    "wanglulu",
                    "王璐璐刘",
                    "0000000",
                    2,
                    new Date(),
                    "18567542321",
                    "北京市朝阳区北苑家园茉莉园20号楼",
                    1,
                    1,
                    new Date(),
                    2,
                    new Date(),
                    40,
                    "666"
            ));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有的用户列表
     */
    @RequestMapping("/list")
    public String list(Model model) {
        logger.info("当查询条件为空时，查询用户信息" + this);
        queryUserList.clear();
        queryUserList.addAll(userList);
        model.addAttribute("userList", queryUserList);  // 使用 model 添加属性
        return "sysUser/sysUserList";  // 返回视图名称
    }

    /**
     * 根据用户名称查询用户列表
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam (value = "realName", required = false) String realName,
            Model model) {
        logger.info("查询条件【realName】=" + realName + "查询用户信息");
        queryUserList.clear();
        if (realName != null && !realName.equals("")) {
            for (SysUser user : userList) {
                if (user.getRealName().equals(realName)) {
                    queryUserList.add(user);
                }
            }
        } else {
            queryUserList.addAll(userList);
        }
        model.addAttribute("userList", queryUserList);  // 使用 model 添加属性
        return "sysUser/sysUserList";  // 返回视图名称
    }
}